//
//  SISSUploadIntermediaryContractViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/5.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSUploadIntermediaryContractViewController.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSHouseProgressModel.h"
#import "SISSHouseProgressRecordInfoModel.h"
#import "SISSPassengerProgressModel.h"
#import "SISSPassengerProgressRecordInfoModel.h"
#import "SISSIntermediaryContractModel.h"

@interface SISSUploadIntermediaryContractViewController ()

@end

@implementation SISSUploadIntermediaryContractViewController

- (void)viewDidLoad {
    self.isHiddenDateChooseView = YES;
    
    [super viewDidLoad];
    
    self.title = @"居间服务合同";
    
    if (self.intermediaryContractModel) {
        self.contractNoView.rightTextField.text = self.intermediaryContractModel.contractNum;
        self.remarkView.rightTextField.text = self.intermediaryContractModel.otherdesc;
        self.uploadImageView.imageUrlArr = self.intermediaryContractModel.image.mutableCopy;
    }
}

- (void)confirmButtonClickAction {
    [self.view endEditing:YES];
    
    if (self.contractNoView.rightTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入合同编号" andTime:2 andColor:YES];
        return;
    }
    if (!self.intermediaryContractModel && self.uploadImageView.images.count == 0) {
        [SISSTools showSVErrorTitle:@"请添加合同文本图片" andTime:2 andColor:YES];
        return;
    }
    NSString *ahbpId = self.houseProgressModel ? self.houseProgressModel.ahbpId : self.passengerProgressRecordInfoModel.ahbpId;
    NSString *adbpId = self.passengerProgressModel ? self.passengerProgressModel.adbpId : self.houseProgressRecordInfoModel.adbpId;
    NSInteger houseType = self.houseProgressModel ? 1 : self.passengerProgressRecordInfoModel.tradeBusinessType;
    NSInteger demandType = self.passengerProgressModel ? 1 : self.houseProgressRecordInfoModel.tradeBusinessType;
    NSString *otherdesc = self.remarkView.rightTextField.text ? self.remarkView.rightTextField.text : @"";
    if (houseType == 2) ahbpId = self.passengerProgressRecordInfoModel.ahrId;
    if (demandType == 2) adbpId = self.houseProgressRecordInfoModel.abdId;
    
    if (self.intermediaryContractModel) {
        
        NSDictionary *parameters = @{
                                     @"id" : self.intermediaryContractModel.ID,
                                     @"ahbpId" : ahbpId,
                                     @"adbpId" : adbpId,
                                     @"contractNum" : self.contractNoView.rightTextField.text,
                                     @"houseType" : @(houseType),
                                     @"demandType" : @(demandType),
                                     @"otherdesc" : otherdesc
                                     };
        
        [kNetworkManager updateIntermediaryContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] == 200) {
                [self pushToSuccessViewWithPopBackClassType: self.houseProgressModel ? SISSProgressPopBackClassTypeHouse : SISSProgressPopBackClassTypePassenger];
            }
            
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        NSDictionary *parameters = @{
                                     @"ahbpId" : ahbpId,
                                     @"adbpId" : adbpId,
                                     @"contractNum" : self.contractNoView.rightTextField.text,
                                     @"house_type" : @(houseType),
                                     @"demand_type" : @(demandType),
                                     @"otherdesc" : otherdesc
                                     };
        [kNetworkManager insertIntermediaryContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] == 200) {
                [self pushToSuccessViewWithPopBackClassType: self.houseProgressModel ? SISSProgressPopBackClassTypeHouse : SISSProgressPopBackClassTypePassenger];
            }
            
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    
}

@end
